package com.cf.controller;


import com.cf.mapper.StudentMapper;
import com.cf.pojo.Student;
import org.junit.Test;
import org.springframework.transaction.support.TransactionSynchronizationAdapter;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import java.util.Date;
import java.util.concurrent.ExecutorService;

/**
 * @author Chengfei
 * @Description
 * @date 2023/6/19
 */
public class DemoController {
    // https://juejin.cn/post/7034398528067764231
/*
    public static void main(String[] args) throws Exception {
        // 开始时间
        long begin = System.currentTimeMillis();
        String docFile = "D:\\PublicSoftware\\jar\\xs.docx";
        String pdfFile = "D:\\PublicSoftware\\jar\\xs7.pdf";

*/
/*        InputStream doc = new FileInputStream(docFile);
        XWPFDocument document = new XWPFDocument(doc);
        PdfOptions options = PdfOptions.create();
        OutputStream out = new FileOutputStream(pdfFile);
        PdfConverter.getInstance().convert(document, out, options);
        doc.close();
        out.close();*//*


        wordToPdf(docFile,pdfFile);

        // 结束时间
        long end = System.currentTimeMillis();
            System.out.println("word转pdf完成: 耗时 : "+ ((end-begin)/1000) + "秒");


        */
/*
        //实例化Document类的对象
        Document doc = new Document();
        //加载Word
        doc.loadFromFile(docFile);

        // 开始时间
        long begin = System.currentTimeMillis();
        //保存为PDF格式
        doc.saveToFile(pdfFile, FileFormat.PDF);
        // 结束时间
        long end = System.currentTimeMillis();
        System.out.println("word转pdf完成: 耗时 : "+ ((end-begin)/1000) + "秒");
*//*


*/
/*        //实例化Document类的对象
        Document doc = new Document();

        //加载Word
        doc.loadFromFile(docFile);

        ToPdfParameterList toPdfParameterList = new ToPdfParameterList();
        // 嵌入所有字体
        toPdfParameterList.isEmbeddedAllFonts(true);
        // 删除超链接 并保留字符格式
        toPdfParameterList.setDisableLink(true);
        // 将输出图像质量设置为原始图像的40% 默认值是80%
        doc.setJPEGQuality(40);

        //保存为PDF格式
        doc.saveToFile(pdfFile, FileFormat.PDF);*//*

    }

    public static void wordToPdf(String wordFile, String pdfFile) throws Exception {
        Document wordDoc = new Document(wordFile);

        InputStream is = new FileInputStream("..license.xml");
        License license = new License();
        license.setLicense(is);


        PdfSaveOptions pso = new PdfSaveOptions();
        wordDoc.save(pdfFile, pso);
    }


    */
/**
     * 修改pdf jar包里面的校验
     *//*

    public static void modifyPDFJar() {
        try {
            //这一步是完整的jar包路径,选择自己解压的jar目录
            ClassPool.getDefault().insertClassPath("D:\\aspose.pdf-21.11-java\\lib\aspose.pdf-21.11.jar");
            //获取指定的class文件对象
            CtClass zzZJJClass = ClassPool.getDefault().getCtClass("com.aspose.pdf.l9f");
            //从class对象中解析获取所有方法
            CtMethod[] methodA = zzZJJClass.getDeclaredMethods();
            for (CtMethod ctMethod : methodA) {
                //获取方法获取参数类型
                CtClass[] ps = ctMethod.getParameterTypes();
                //筛选同名方法，入参是Document
                if (ps.length == 1 && ctMethod.getName().equals("lI") && ps[0].getName().equals("java.io.InputStream")) {
                    System.out.println("ps[0].getName==" + ps[0].getName());
                    //替换指定方法的方法体
                    ctMethod.setBody("{this.l0if = com.aspose.pdf.l10if.lf;com.aspose.pdf.internal.imaging.internal.p71.Helper.help1();lI(this);}");
                }
            }
            //这一步就是将破译完的代码放在桌面上
            zzZJJClass.writeFile("C:\\Users\\roc\\Desktop\\");

        } catch (Exception e) {
            System.out.println("错误==" + e);
        }
    }
*/


    StudentMapper studentMapper = new StudentMapper() {
    @Override
    public void updateById(Student student) {

        boolean actualTransactionActive = TransactionSynchronizationManager.isActualTransactionActive();
        if (!actualTransactionActive){

        }

        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
            @Override
            public void afterCommit() {
                // 异步操作
                executorService.execute(new Runnable() {
                    @Override
                    public void run() {

                    }
                });
            }
        });

    }
};

    private ExecutorService executorService;

    @Test
    public void save(){

        // 保存用户
        saveUser();

        // 判断当前线程是否存在活跃状态的事务
        boolean actualTransactionActive = TransactionSynchronizationManager.isActualTransactionActive();

        // 不存在则事务都完成
        if (!actualTransactionActive){
            // 异步操作 发送消息
            executorService.execute(() -> sendMessage());
        }else{
            // 存在活跃事务, 则监听事务, afterCommit是指事务提交完再执行
            TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
                @Override
                public void afterCommit() {
                   // 异步操作 发送消息
                    executorService.execute(() -> sendMessage());
                }
            });
        }

}


    public void saveUser(){

    }

    public void sendMessage(){

    }

    @Test
    public void testUpdateById(){
        // 参数从前端传递,此处模拟获取
        Student student = new Student();
        student.setId("1111");
        student.setName("小明");
        student.setAge(18);
        student.setCreatTime(new Date());
        // 模拟前端更新 删除了备注信息
        student.setDesc("");
        // 模拟前端更新 将字段指为空传递
        student.setScore(null);

        studentMapper.updateById(student);
    }

}
